﻿using System;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Data.SqlClient;

namespace RESTAURANT
{
    [Serializable]
    public class Connection
    {
        public string connectionString;
        public string serverConnectionString;
        public string masterConnectionString;
    }
    public class Configurations
    {
        string connectionString;
        string serverConnectionString;
        string masterConnectionString;

        public static bool CreateConnectionConfig(string filename, string server, string user, string pass)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            SqlConnection sqlcon = new SqlConnection();
            try
            {
                
                Connection conn = new Connection();

                sqlcon.ConnectionString = conn.connectionString = @"Server=" + server + ";Database=RESTAURANT;Trusted_Connection=False;User ID=" + user + ";Password=" + pass + ";";
                sqlcon.Open();
                sqlcon.Close();

                sqlcon.ConnectionString = conn.serverConnectionString = @"Server=" + server + ";Trusted_Connection=False;User ID=" + user + ";Password=" + pass + ";Pooling=False";
                sqlcon.Open();
                sqlcon.Close();

                sqlcon.ConnectionString = conn.masterConnectionString = @"Server=" + server + ";Database=Master;Trusted_Connection=False;User ID=" + user + ";Password=" + pass + ";Pooling=False";
                sqlcon.Open();
                sqlcon.Close();

                FileStream output = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
                formatter.Serialize(output, conn);
                output.Close();

                return true;
            }
            catch(Exception)
            {
                return false;
            }
        }
        public static bool CreateConnectionConfig(string filename, string server)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            SqlConnection sqlcon = new SqlConnection();
            try
            {
                
                Connection conn = new Connection();

                sqlcon.ConnectionString = conn.connectionString = @"Server=" + server + ";Database=RESTAURANT;Trusted_Connection=True;";
                sqlcon.Open();
                sqlcon.Close();

                sqlcon.ConnectionString = conn.serverConnectionString = @"Server=" + server + ";Trusted_Connection=True;Pooling=False";
                sqlcon.Open();
                sqlcon.Close();

                sqlcon.ConnectionString = conn.masterConnectionString = @"Server=" + server + ";Database=Master;Trusted_Connection=True;Pooling=False";
                sqlcon.Open();
                sqlcon.Close();

                FileStream output = new FileStream(filename, FileMode.Create, FileAccess.Write);
                formatter.Serialize(output, conn);
                output.Close();

                return true;
            }
            catch
            {
                return false;
            }
        }
        public Configurations()
        {
            connectionString = @"Server=.\SQLEXPRESS;Database=RESTAURANT; Trusted_Connection=True;";
            serverConnectionString = @"Server=.\SQLEXPRESS; Trusted_Connection=True;";
            masterConnectionString = @"Server=.\SQLEXPRESS; Database=master;Trusted_Connection=True;";
        }
        public Configurations(string filename)
        {
            try
            {
                BinaryFormatter reader = new BinaryFormatter();
                FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read);
                Connection conn = (Connection)reader.Deserialize(input);

                connectionString = conn.connectionString;
                serverConnectionString = conn.serverConnectionString;
                masterConnectionString = conn.masterConnectionString;
            }
            catch
            {

            }
        }

        public String ConnectionString
        {
            get
            {
                return connectionString;
            }
        }
        public String ServerConnectionString
        {
            get
            {
                return serverConnectionString;
            }
        }
        public String MasterConnectionString
        {
            get
            {
                return masterConnectionString;
            }
        }
    }
}
